package soapApiDemo;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
class AddHeaderToSoap
{
	private static final String LocalName = "TimeRequest";
    private static final String Namespace = "http://ch01/mysoap/";
    private static final String NamespacePrefix = "ms";
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SOAPMessage msg = null;
	       try {
	         MessageFactory mf = MessageFactory.newInstance();
	         msg = mf.createMessage();
	         
	         SOAPEnvelope env = msg.getSOAPPart().getEnvelope();
	         System.out.println("env="+env.getAttribute("xmlns:SOAP-ENV")+"--");
	         msg.writeTo(System.out);
	         SOAPHeader hdr = env.getHeader();
	         Name lookup_name = env.createName(LocalName,NamespacePrefix,Namespace);
	         hdr.addHeaderElement(lookup_name).addTextNode("time_request");
	         ByteArrayOutputStream out = new ByteArrayOutputStream();
	         System.out.println("--"); 
	         msg.writeTo(out);
	         System.out.println(""+out.size()+""+out.toString());
	         msg.writeTo(System.out);
	         }
	       catch(IOException e) { System.err.println(e); }
	         catch(SOAPException e) { System.err.println(e); }
	       catch(Exception e) { System.err.println(e); }
	}
}
